package com.jyh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.dbUser.dao.PersonInfoDao;
import com.jyh.util.StringUtils;

@Service
public class PersonOrderService extends BaseAction {
	@Autowired
	private PersonInfoDao personInfoDao;

	/**
	 * 个人基础信息 from Order
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView findPersonInfo(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("user/detailInfo");

		String orderNo = request.getParameter("orderNO");
		String sql = "  SELECT userNumber AS userNumber,loginNo AS phoneNumber"
				+ ",cardNo AS userIDCard, maxEducation AS educational,userName AS userName,"
				+ "sex AS sex,marriageState AS marriage,province AS livingProvinces ,dateBirth "
				+ "AS birDate,city AS livingCity,qqNo AS QQNo ,address AS detailedAddress,age "
				+ "AS age FROM jyh_user_msg WHERE 1=1 ";
		if (StringUtils.isNotNull(orderNo)) {
			sql = sql + " and orderNo='" + orderNo + "'";
		}

		Map<String, Object> map = personInfoDao.getMap(sql);
			
		map.putAll(workInfo(orderNo));
		map.putAll(contactInfo(orderNo));
		map.putAll(bankCardInfo(orderNo));
		map.putAll(authInfo(orderNo));

		List<Map<String, Object>> lis = new ArrayList<>();
		lis.add(map);

		mv.addObject("list", lis);
		return mv;
	}

	/**
	 * 工作信息 TODO
	 */
	public Map<String, Object> workInfo(String orderNo) {
		String sql = "SELECT unitName AS companyName,province AS companyProvinces,occupation AS occupation,city AS companyCity,"
				+ "unitPhone AS companyTelephone,monthlyIncome AS monthIncome,address AS companyAddress,payday AS salaryDay  "
				+ "FROM jyh_user_work_msg WHERE 1=1 ";
		if (StringUtils.isNotNull(orderNo)) {
			sql = sql + " and orderNo='" + orderNo + "'";
		}
		Map<String, Object> map = personInfoDao.getMap(sql);

		return map;
	}

	/**
	 * 联系人信息
	 */
	public Map<String, Object> contactInfo(String orderNo) {
		String sql = "SELECT CASE contactType WHEN 0 THEN N'直系亲属' ELSE N'未知' END directRelatives, contactName AS RelativesName,contactPhone "
				+ "AS RelativesPhoneNo ,orderNo FROM jyh_user_contact WHERE contactType='0' ";
		if (StringUtils.isNotNull(orderNo)) {
			sql = sql + " and orderNo='" + orderNo + "'";
		}

		String sql2 = " SELECT CASE contactType WHEN 1 THEN N'同事朋友' ELSE N'未知' END friendColleague, contactName AS friendName,contactPhone "
				+ "AS friendPhoneNo ,orderNo FROM jyh_user_contact WHERE contactType='1' ";
		if (StringUtils.isNotNull(orderNo)) {
			sql2 = sql2 + " and orderNo='" + orderNo + "'";
		}

		Map<String, Object> map = new HashMap<>();
		Map<String, Object> map1 = personInfoDao.getMap(sql);
		Map<String, Object> map2 = personInfoDao.getMap(sql2);
		map.putAll(map1);
		map.putAll(map2);

		return map;
	}

	/**
	 * 银行卡信息 list<map>
	 */
	public Map<String, Object> bankCardInfo(String orderNo) {
		String sql = " SELECT userName AS cardholder,bankName AS bankName,bankCardNo AS bankCard,"
				+ "withhold AS binding FROM jyh_user_bank WHERE 1=1  ";
		if (StringUtils.isNotNull(orderNo)) {
			sql = sql + " and orderNo='" + orderNo + "'";
		}
		List<Map<String, Object>> listMap = personInfoDao.getListMap(sql);
		Map<String, Object> map = new HashMap<>();
		map.put("listmap", listMap);
		return map;
	}

	/**
	 * 认证信息
	 */
	public Map<String, Object> authInfo(String orderNo) {
		String sql = "SELECT zimaFen ,operatorType,ascription,phone, CASE operator WHEN 0 THEN N'未通过' "
				+ "WHEN 1 THEN N'通过' ELSE N'未知' END operator,CASE idCardZ WHEN '无' "
				+ "THEN N'未认证' ELSE N'已认证' END idCardZ FROM jyh_user_auth WHERE 1=1 ";
		if (StringUtils.isNotNull(orderNo)) {
			sql = sql + " and orderNo='" + orderNo + "'";
		}
		Map<String, Object> map = personInfoDao.getMap(sql);

		return map;

	}
}
